package cn.iocoder.yudao.module.game.service.coinLog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.coinLog.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.coinLog.CoinLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.coinLog.CoinLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 金币日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CoinLogServiceImpl implements CoinLogService {

    @Resource
    private CoinLogMapper coinLogMapper;

    @Override
    public Long createCoinLog(CoinLogSaveReqVO createReqVO) {
        // 插入
        CoinLogDO coinLog = BeanUtils.toBean(createReqVO, CoinLogDO.class);
        coinLogMapper.insert(coinLog);
        // 返回
        return coinLog.getId();
    }

    @Override
    public void updateCoinLog(CoinLogSaveReqVO updateReqVO) {
        // 校验存在
        validateCoinLogExists(updateReqVO.getId());
        // 更新
        CoinLogDO updateObj = BeanUtils.toBean(updateReqVO, CoinLogDO.class);
        coinLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteCoinLog(Long id) {
        // 校验存在
        validateCoinLogExists(id);
        // 删除
        coinLogMapper.deleteById(id);
    }

    private void validateCoinLogExists(Long id) {
        if (coinLogMapper.selectById(id) == null) {
            throw exception(COIN_LOG_NOT_EXISTS);
        }
    }

    @Override
    public CoinLogDO getCoinLog(Long id) {
        return coinLogMapper.selectById(id);
    }

    @Override
    public PageResult<CoinLogDO> getCoinLogPage(CoinLogPageReqVO pageReqVO) {
        return coinLogMapper.selectPage(pageReqVO);
    }

}